package method.gridviewPhoto;

import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.*;

import view.Core.*;
import android.app.Activity;


public class GridViewConfig {
    private static ArrayList resim_list=new ArrayList();
    private static String URLlink ;
    private static String URLlinkB ;
    
    public static void setURLlink(String link, String linkB){
    	URLlink = link;
    	URLlinkB = linkB;
    }
    
    public static ArrayList getResim_list() {
        return resim_list;
    }
 
    public static void setResim_list(ArrayList resim_list) {
        GridViewConfig.resim_list = resim_list;
    }
 
    public static void clean(){
    	resim_list.clear();
    }
    public static void addImageUrls(String treeID){
		try {
			// String url =
			// "http://192.168.1.101/oldTreeWeb/getJsonField.php?Name=Tree tat";
			//http://localhost/oldTreeWeb/getPhotoID.php?inputTreeID=T0002
			//String input = getResources().getString(R.string.serverURL)+"getPhotoID.php?inputTreeID="+treeID;
			String input = URLlink+"getPhotoID.php?inputTreeID="+treeID;
			//String input = "http://192.168.1.101/oldTreeWeb/getPhotoID.php?inputTreeID="+treeID;
			//Resources.getSystem().getString(android.R.string.serverURL)
			//getResources().getString(R.string.serverURL)
			String url = input;

			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(response.getEntity()
							.getContent()));
			while ((line = reader.readLine()) != null) {
				reply += line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);
				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				
				//List<String> list = new ArrayList();
				//String URLphoto = getResources().getString(R.string.URLphoto);
				JSONObject jsonField;
				for(int i=0;i< jsonArrayB.length();i++){
					jsonField = new JSONObject(jsonArrayB.getString(i));
					//x = jsonField.getString("photoID")+x;
					//list.add(URLphoto+jsonField.getString("photoID"));
					//resim_list.add("http://192.168.1.101/treewebsite/treephoto/"+jsonField.getString("photo"));
					resim_list.add(URLlinkB+"treephoto/"+jsonField.getString("photo"));
				}
				
			} catch (JSONException e) {
				//textView1.setText("JSONException");
			}
		} catch (Exception e) {
			//textView1.setText("Please Check network connection");
		}
 

         
    }

}
